package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {

    @Autowired
    private EmpService empService;

    /**
     * 分页查询班级列表
     *
     * @param param 查询参数
     * @return 分页查询结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<Clazz> getPageResult(ClazzQueryParam param) {

        Page<Clazz> page = new Page<>(param.getPage(), param.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));

        page = lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(), Clazz::getName, param.getName())
                .between(param.getBegin() != null && param.getEnd() != null, Clazz::getEndDate, param.getBegin(), param.getEnd())
                .page(page);

        List<Clazz> clazzs = page.getRecords();

        List<Integer> masterIds = page.getRecords().stream().map(Clazz::getMasterId).collect(Collectors.toList());

        if (!masterIds.isEmpty()) {
            List<Emp> Emps = empService.listByIds(masterIds);
            clazzs.forEach(clazz -> {
                Emps.forEach(emp -> {
                    if (clazz.getMasterId() != null && clazz.getMasterId().equals(emp.getId())) {
                        clazz.setMasterName(emp.getName());
                    }
                });
            });
        }

        clazzs.forEach(clazz -> {
            if(clazz.getBeginDate().isAfter(LocalDate.now())) {
                clazz.setStatus("未开班");
            } else if (clazz.getEndDate().isAfter(LocalDate.now())) {
                clazz.setStatus("已开班");
            } else {
                clazz.setStatus("已结课");
            }
        });

        return new PageResult<>(page.getTotal(), clazzs);
    }

    /**
     * 保存班级信息
     */
    @Override
    public void saveClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        save(clazz);
    }

    /**
     * 根据id查询班级信息
     *
     * @param id 班级id
     * @return 班级信息
     */
    @Override
    public Clazz getClazzById(Integer id) {
        return getById(id);
    }

    /**
     * 修改班级信息
     *
     * @param clazz 班级信息
     */
    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        updateById(clazz);
    }


}
